package me.yex.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.yex.mall.common.utils.PageUtils;
import me.yex.mall.common.utils.R;
import me.yex.mall.product.entity.CategoryEntity;
import me.yex.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品三级分类
 *
 * @author yex
 * @email yex0123@qq.com
 * @date 2021-08-13 00:41:15
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    public R treeList(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> categoryEntityList = categoryService.list(new QueryWrapper<>());

        Map<Integer, List<CategoryEntity>> catLevelMap = categoryEntityList.stream().collect(Collectors.groupingBy(CategoryEntity::getCatLevel));

        for (CategoryEntity categoryEntity2 : catLevelMap.get(2)) {
            List<CategoryEntity> category3List = catLevelMap.get(3).stream()
                    .filter(c -> c.getParentCid().equals(categoryEntity2.getCatId()))
                    .sorted(Comparator.comparingInt(CategoryEntity::getSort))
                    .collect(Collectors.toList());
            categoryEntity2.setChildLevelList(category3List);
        }

        for (CategoryEntity categoryEntity1 : catLevelMap.get(1)) {
            List<CategoryEntity> category2List = catLevelMap.get(2).stream()
                    .filter(c -> c.getParentCid().equals(categoryEntity1.getCatId()))
                    .sorted(Comparator.comparingInt(CategoryEntity::getSort))
                    .collect(Collectors.toList());
            categoryEntity1.setChildLevelList(category2List);
        }
        List<CategoryEntity> data = catLevelMap.get(1).stream()
                .sorted(Comparator.comparingInt(CategoryEntity::getSort))
                .collect(Collectors.toList());

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
